<?
require_once("mvc/action/Action.php");
require_once("cms/core/om/CategoryPeer.php");
require_once("cms/core/om/CategoryI18nPeer.php");
require_once("cms/core/helper/Parameter.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $errors = new ActionMessages();

        $categoryI18n =& $form->getCategoryI18n();
        $categoryI18n->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

        $category =& $categoryI18n->getCategory();

        $parameters = new PHP_CMS_View_Helper_Parameter();

        // Set the module set in mvc-config.xml
        $category->setModule($parameters->parameter("module", $mapping));

        $conn = Propel::getConnection();

        // Default language
        $default = $request->getContext()->
            getInitParameter(_Core::DEFAULT_LANGUAGE);

        try {

            $conn->begin();

            // process image
            $formImage =& $form->getFormImage();

            if(!$form->getDeleteImage() && $formImage["size"] > 0) {

                $image = $category->getImageRelatedByImageId();
                $thumb = $category->getImageRelatedByThumbnailId();

                require_once("cms/core/util/ImageProperties.php");
                $properties = new ImageProperties(
                    $request->getContext()->getInitParameter(_Core::SETTING_CATEGORY)
                );

                require_once("cms/core/util/ImageProcessor.php");
                ImageProcessor::process(
                    $conn, $errors, $formImage,
                    $properties, $image, $thumb
                );

                // Set the generated ids
                $category->setThumbnailId($thumb->getImageId());
                $category->setImageId($image->getImageId());

            } else if($form->getDeleteImage()) {

                $image = $category->getImageRelatedByImageId();
                $thumb = $category->getImageRelatedByThumbnailId();

                require_once("cms/core/om/BinaryFilePeer.php");

                BinaryFilePeer::doDelete($thumb->getBinaryFile(), $conn);
                BinaryFilePeer::doDelete($image->getBinaryFile(), $conn);
            }

            // save
            if($categoryI18n->getCategoryI18nId() > 0) {

                CategoryI18nPeer::doUpdate($categoryI18n, $conn);

                $category->setCategoryId($categoryI18n->getCategoryId());

                if($categoryI18n->getLanguageCode() == $default)
                    $category->setUri(URL::sanitize_title_with_dashes($categoryI18n->getName()));

                CategoryPeer::doUpdate($category, $conn);

            } else {

                // An initial default language pageCategory is added.
                if(strlen($categoryI18n->getCategoryId()) == 0) {

                    $category->setUri(URL::sanitize_title_with_dashes($categoryI18n->getName()));
                    $category->setPosition($category->getMaxPosition() + 1);

                    $categoryI18n->setCategoryId(
                        CategoryPeer::doInsert($category, $conn));

                    $categoryI18n->setLanguageCode($default);

                }
                $categoryI18n->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                $categoryI18n->setActive(_Core::AUTO_ACTIVATE);
                CategoryI18nPeer::doInsert($categoryI18n, $conn);

            }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.category.save", true));
            $this->log->error($e->getMessage());
        }


        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }

        $session  = $request->getSession();
        $listForm = $session->getAttribute($parameters->parameter("listform", $mapping));
        if(!is_null($listForm)) {
            $listForm->setPage(1);
            $session->setAttribute($parameters->parameter("listform", $mapping), $listForm);
        }
        
        if($request->getContext()->getInitParameter(_Core::CACHE)) {
            // Get the cache
            $cache = $request->getContext()->getCache();
            // Clear the menu cache
            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
            // Clear the page cache
            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
        }

        return $mapping->findForward("success");


    }

}
?>